package com.yazi.mxz.common.service.impl;


import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.myframework.smhj.hibernate.dao.BasicService;
import com.myframework.smhj.hibernate.dao.Dao;
import com.yazi.mxz.common.db.dao.MemberPromoteDao;
import com.yazi.mxz.common.entity.ValueFactory;
import com.yazi.mxz.common.entity.beans.Company;
import com.yazi.mxz.common.entity.beans.MemberPromote;
import com.yazi.mxz.common.entity.enums.MemberPromoteType;
import com.yazi.mxz.common.service.MemberPromoteService;

@Service("memberPromoteService")
public class MemberPromoteServiceImpl extends BasicService<MemberPromote> implements MemberPromoteService {
	private static Logger log = LoggerFactory.getLogger(MemberPromoteServiceImpl.class);

	@Resource
	MemberPromoteDao memberPromoteDao;
	
	@Override
	protected Dao<MemberPromote> getDao() {
		return memberPromoteDao;
	}
	
	@Override
	public void addByCompany(Company company, MemberPromoteType type, List<MemberPromote> mps) {
		log.debug("addByCompany: companyId={},type={},size={}", 
				new Object[]{company.getCompanyId(), type, mps.size()});
		
		// 先删除原有同类类促销
		memberPromoteDao.delete(company.getCompanyId(), type.ordinal());
		// 添加新促销信息
		for (MemberPromote mp : mps) {
			ValueFactory.validate(mp);
			mp.setCompany(company);
			mp.setType(type.ordinal());
			mp.setPromoteId(null);
			memberPromoteDao.merge(mp);
		}
	}

}
